*------------------------------------------------------------------------------*
							*** PERMUATIONS TESTS ***
*------------------------------------------------------------------------------*

use "${tempdata}kreise_complete_all.dta", clear

*------------------------------------------------------------------------------*

***Macros

local weather "O3_30km_10ug temp_30km humid_rel_30km wind_speed_30km precip_30km"
local demo_econ "pop_tot GDP_capita employment"
local dep_vars "Incumbent_share"

*------------------------------------------------------------------------------*

***Run permutations

set seed 456341364

foreach y of local dep_vars {

	*Permuations
	permute PM10_30km_10ug _b[PM10_30km_10ug], reps(1000) dots(10) strata(kreis_code) saving("${tempdata}Perm_`y'.dta", replace): ///
		reghdfe `y' PM10_30km_10ug `weather' `demo_econ' turnout, ${hdfe_opt}, if samplePM10_allcontrols == 1
	
	*Save coefficients in separate files
	preserve
		use "${tempdata}Perm_`y'.dta", clear
		rename _pm_1 PM10_`y'
		gen Permutation = _n
		save "${tempdata}Perm_`y'.dta", replace
	restore
	
	*Store usual coefficients in locals
	reghdfe `y' PM10_30km_10ug `weather' `demo_econ' turnout, ${hdfe_opt}, if samplePM10_allcontrols == 1
	mat b = e(b)
	local b_`y' = b[1,1]
	local fmt_b_`y' : di %4.3f `b_`y''
	
} // y

*------------------------------------------------------------------------------*

***Create histogram

use "${tempdata}Perm_Incumbent_share.dta", clear

*Graph
graph twoway (hist PM10_Incumbent_share, bin(20) fcolor(navy) lcolor(ltbluishgray) lwidth(vthin)) || ///
			 (scatteri 0 `b_Incumbent_share' 250 `b_Incumbent_share', c(l) m(i) lwidth(medthick)) || ///
			 (pcarrowi 225 -0.018 225 -0.0205, color(black) text(225 -0.0135 "Estimated effect: `fmt_b_Incumbent_share'"))  , ///
			 xtitle("Change in percentage points") name(incumbent_perm, replace) graphregion(color(white)) legend(off)
graph export "${outputs}figure_S7.pdf", as(pdf) replace

*------------------------------------------------------------------------------*

clear

exit
